﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ADL
{
    public class ADL_admin
    {
        /// <summary>
        /// 增
        /// </summary>
        /// <param name="tb"></param>
        /// <returns></returns>
        public string insert(_admin tb)
        {
            if (tb._admin_id != null)
            {
                CdioDBDataContext da = new CdioDBDataContext();
                da._admin.InsertOnSubmit(tb);
                da.SubmitChanges();
                string contactid = "" + tb._admin_id;
                return contactid;
            }
            return null;
        }

        /// <summary>
        /// 删
        /// </summary>
        /// <param name="tb"></param>
        /// <returns></returns>
        public bool delete(_admin tb)
        {
            CdioDBDataContext da = new CdioDBDataContext();
            var p = da._admin.Where(item => item._admin_id == tb._admin_id);
            if (p.Any())
            {
                da._admin.DeleteOnSubmit(p.ToList()[0]);
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="ct"></param>
        /// <returns></returns>
        public bool update(_admin tb)
        {
            CdioDBDataContext da = new CdioDBDataContext();
            var p = da._admin.Where(item => item._admin_id == tb._admin_id);
            if (p.Any())
            {
                var var_table = p.ToList()[0];
                var_table._admin_id = tb._admin_id;
                var_table._admin_name = tb._admin_name;
                var_table._admin_psw = tb._admin_psw;
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="ct"></param>
        /// <param name="refrenceskey">根据那个键值查询</param>
        /// <returns></returns>
        public List<_admin> search(_admin tb, string refrenceskey)
        {
            CdioDBDataContext da = new CdioDBDataContext();
            switch (refrenceskey)
            {
                case "_admin_id":
                    if (tb._admin_id != null)
                    {
                        var p = da._admin.Where(item => item._admin_id == tb._admin_id);
                        if (p.Any())
                        {
                            return p.ToList();
                        }
                    }
                    break;
                case "_admin_name":
                    if (tb._admin_name != null)
                    {
                        var p = da._admin.Where(item => item._admin_name == tb._admin_name && item._admin_name == tb._admin_name);
                        if (p.Any())
                        {
                            return p.ToList();
                        }
                    }
                    break;
            }
            return null;
        }
    }
}
